/**
 * 生成简单crud
 */

const fs = require('fs');
const path = require('path');
const readline = require('readline');

function titleCase(str) {
    newStr = str.slice(0, 1).toUpperCase() + str.slice(1).toLowerCase();
    return newStr;
}

function run() {
    let entityClassName = '';
    // 创建readline接口实例
    let r1 = readline.createInterface({
        input: process.stdin,
        output: process.stdout,
    });
    //close事件监听
    r1.on('close', function () {
        // 结束程序
        process.exit(0);
    });
    //调用接口方法
    r1.question('请输入类名\t', function (answer) {
        var re = new RegExp('^[a-zA-Z]+$');
        if (re.test(answer)) {
            entityClassName = titleCase(answer);
            generate(entityClassName);
        } else {
            console.log('输入类目错误，请重新输入');
        }
        r1.close();
    });
}

async function generate(entityClassName) {
    let lowEntityClassName = entityClassName.toLowerCase();

    // 1.在src文件夹下创建entityClassName的文件夹
    // fs.mkdirSync(path.join(__dirname, `../${lowEntityClassName}`), {
    //     recursive: true,
    // });
    // fs.mkdirSync(path.join(__dirname, `../${lowEntityClassName}/dto`), {
    //     recursive: true,
    // });
    // fs.mkdirSync(path.join(__dirname, `../${lowEntityClassName}/entities`), {
    //     recursive: true,
    // });
    // 2.读取并写入create.dto
    let filePath = path.join(
        __dirname,
        './src/template/dto/create-template.dto.template',
    );
    let res = fs.readFileSync(filePath).toString();
    res = res.replaceAll('${template}', entityClassName);
    fs.writeFileSync(
        path.join(
            __dirname,
            `/src/dto/create-${lowEntityClassName}.dto.ts`,
        ),
        res,
    );
    // 3.读取并写入update.dto
    filePath = path.join(
        __dirname,
        './src/template/dto/update-template.dto.template',
    );
    res = fs.readFileSync(filePath).toString();
    res = res.replaceAll('${template}', entityClassName);
    res = res.replaceAll('template', lowEntityClassName);
    fs.writeFileSync(
        path.join(
            __dirname,
            `/src/dto/update-${lowEntityClassName}.dto.ts`,
        ),
        res,
    );
    // 4.读取并写入entities
    filePath = path.join(
        __dirname,
        './src/template/entities/template.entity.template',
    );
    res = fs.readFileSync(filePath).toString();
    res = res.replaceAll('${template}', entityClassName);
    res = res.replaceAll('template', lowEntityClassName);
    fs.writeFileSync(
        path.join(
            __dirname,
            `/src/entity/${lowEntityClassName}.entity.ts`,
        ),
        res,
    );
    // 5.读取并写入service
    filePath = path.join(__dirname, './src/template/template.service.template');
    res = fs.readFileSync(filePath).toString();
    res = res.replaceAll('${template}', entityClassName);
    res = res.replaceAll('template', lowEntityClassName);
    fs.writeFileSync(
        path.join(
            __dirname,
            `/src/service/${lowEntityClassName}.service.ts`,
        ),
        res,
    );
    // 6.读取并写入controller
    filePath = path.join(__dirname, './src/template/template.controller.template');
    res = fs.readFileSync(filePath).toString();
    res = res.replaceAll('${template}', entityClassName);
    res = res.replaceAll('template', lowEntityClassName);
    fs.writeFileSync(
        path.join(
            __dirname,
            `/src/admin/${lowEntityClassName}.controller.ts`,
        ),
        res,
    );
    // 6.读取并写入controller
    filePath = path.join(__dirname, './src/template/template.api.template');
    res = fs.readFileSync(filePath).toString();
    res = res.replaceAll('${template}', entityClassName);
    res = res.replaceAll('template', lowEntityClassName);
    fs.writeFileSync(
        path.join(
            __dirname,
            `/src/api/${lowEntityClassName}.api.ts`,
        ),
        res,
    );
    // 7.读取并写入module
    filePath = path.join(__dirname, './src/template/template.module.template');
    res = fs.readFileSync(filePath).toString();
    res = res.replaceAll('${template}', entityClassName);
    res = res.replaceAll('template', lowEntityClassName);
    fs.writeFileSync(
        path.join(
            __dirname,
            `/src/module/${lowEntityClassName}.module.ts`,
        ),
        res,
    );
    // 8.读取并写入vue
    filePath = path.join(__dirname, './src/template/vue/index.template');
    res = fs.readFileSync(filePath).toString();
    res = res.replaceAll('${template}', lowEntityClassName);
    fs.writeFileSync(
        path.join(__dirname, `/public/${lowEntityClassName}.vue`),
        res,
    );
}

run();

module.export = { titleCase };
